/*
 * SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2023. All rights reserved.
 * Description   : udk memzone interface
 */

#ifndef UDK_MEMZONE_H
#define UDK_MEMZONE_H

#include "udk_common.h"

#ifdef __cplusplus
extern "C" {
#endif

#define UDK_MAX_MEMZONE 2560

#define UDK_MEMZONE_2MB            0x00000001
#define UDK_MEMZONE_1GB            0x00000002
#define UDK_MEMZONE_16MB           0x00000100
#define UDK_MEMZONE_16GB           0x00000200
#define UDK_MEMZONE_256KB          0x00010000
#define UDK_MEMZONE_256MB          0x00020000
#define UDK_MEMZONE_512MB          0x00040000
#define UDK_MEMZONE_4GB            0x00080000
#define UDK_MEMZONE_SIZE_HINT_ONLY 0x00000004
#define UDK_MEMZONE_IOVA_CONTIG    0x00100000

#define UDK_MEMZONE_NAMESIZE 32 /* Maximum length of memory zone name. */

struct udk_memzone {
    char name[UDK_MEMZONE_NAMESIZE];  /* Name of the memory zone. */

    uint64_t iova;                    /* Start IO address. */

    UDK_STD_C11
    union {
        void *addr;                   /* Start virtual address. */
        uint64_t addr_64;             /* Makes sure addr is always 64-bits */
    };
    size_t len;                       /* Length of the memzone. */
    uint64_t hugepage_sz;             /* The page size of underlying memory */
    int32_t socket_id;                /* NUMA socket ID. */
    uint32_t flags;                   /* Characteristics of this memzone. */
} __attribute__((__packed__));

const struct udk_memzone *udk_memzone_lookup(const char *name);
const struct udk_memzone *udk_memzone_reserve(const char *name, size_t len, int socket_id, uint32_t flags);
const struct udk_memzone *udk_memzone_reserve_aligned(const char *name, size_t len,
    int socket_id, uint32_t flags, uint32_t align);
int udk_memzone_free(const struct udk_memzone *mz);
int udk_memzone_init(void);
void udk_memzone_deinit(void);
void udk_memzone_walk(void (*func)(const struct udk_memzone *, void *), void *arg);
void udk_memzone_dump(FILE *f);

#ifdef __cplusplus
}
#endif

#endif
